package com.hyk.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.databind.util.BeanUtil;
import com.hyk.dto.ApiResult;
import com.hyk.entity.CodeGeneratorX;
import com.hyk.entity.User;
import com.hyk.entity.request.CodeGeneratorRequest;
import com.hyk.entity.response.CodeGeneratorResponse;
import com.hyk.service.ICodeGeneratorXService;
import com.hyk.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

import static com.hyk.controller.BaseController.getUser;

/**
 * <p>
 * 代码生成器表 前端控制器
 * </p>
 *
 * @author hyk
 * @since 2021-03-10
 */
@RestController
@RequestMapping("/code-generator-x")
@Api(tags = "代码生成器表 前端控制器")
@CrossOrigin
public class CodeGeneratorXController {

    @Resource
    private ICodeGeneratorXService codeGeneratorXService;

    @PostMapping("/alllist")
    @PreAuthorize("hasRole('ADMIN')")
    @ApiOperation("获取所有代码生成器列表")
    public ApiResult allList(){

        List<CodeGeneratorResponse> codeGeneratorXES = codeGeneratorXService.allList();
        return R.ok(codeGeneratorXES);
    }

    @PostMapping("/list")
    @ApiOperation("获取代码生成器列表")
    public ApiResult list(@RequestBody CodeGeneratorRequest request){

        List<CodeGeneratorResponse> codeGeneratorXES = codeGeneratorXService.listFor(request);
        return R.ok(codeGeneratorXES);
    }

    @PostMapping("/add")
    @ApiOperation("添加代码生成器数据")
    public ApiResult add(@RequestBody CodeGeneratorRequest request,
                         HttpServletRequest httpServletRequest){

        User user = getUser(httpServletRequest);

        CodeGeneratorX codeGeneratorX = new CodeGeneratorX();
        BeanUtils.copyProperties(request,codeGeneratorX);

        codeGeneratorX.setCreateTime(new Date());
        codeGeneratorX.setUpdateTime(new Date());

        codeGeneratorXService.save(codeGeneratorX);
        return R.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改代码生成器数据")
    public ApiResult update(@RequestBody CodeGeneratorRequest request){
        QueryWrapper<CodeGeneratorX> wrapper = new QueryWrapper<>();

        wrapper.eq("id",request.getId());

        codeGeneratorXService.update(wrapper);
        return R.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除代码生成器数据")
    public ApiResult delete(@RequestBody CodeGeneratorRequest request){

        codeGeneratorXService.removeById(request.getId());
        return R.ok();

    }

    @PostMapping("/execute")
    @ApiOperation("执行代码生成器")
    public ApiResult execute(@RequestBody CodeGeneratorRequest request){

        codeGeneratorXService.execute(request.getId());
        return R.ok();

    }

}
